#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "Ultrasonic.h"
#include "stdio.h"
#include "iic.h"
#include "LED.h"
unsigned long ulms;
unsigned char pucSeg_Buf[9],pucSeg_Code[8],ucSeg_Pos;
unsigned int ucSeg_Dly;
unsigned char ucKey_Dly,ucKey_Old;
unsigned char ucState;
unsigned char ucWave,ucWave_Num;
unsigned char ucNum_Flag,ucNumHL_Flag,ucNumL=10,ucNumH=60;
unsigned char ucpcf_U;
unsigned char ucWave_Old=1,ucWave_Flag;
unsigned char ucLed,ucLed_Flag,ucLed_N;
void Key_Proc(void);
void Seg_Proc(void);
void Num_Proc(unsigned char ucKey_Down);
void Led_Proc(void);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Timer0Init();
	while(1){
		Key_Proc();
		Seg_Proc();
		Led_Proc();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	 if(!(ulms % 100)) 
	 { 
		 ucLed_N^=1;
	} 
	 Led_Disp(ucLed) ;
	 if(++ucKey_Dly==10) ucKey_Dly=0;
	if(++ucSeg_Dly==500) ucSeg_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 switch(ucKey_Down){
		 case 4:
			 if(++ucState==3) ucState=0;
			 break;
		 case 5:
			 if(ucState==1){
				 ucNum_Flag^=1;
				 ucNumHL_Flag=0;
			 }
			if(ucState==2){
				ucWave_Num=0;
			}
			 break;
		 case 8:
			 Num_Proc(ucKey_Down);
			break;
		  case 9:
			 Num_Proc(ucKey_Down);
			break;
	 }
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	if(ucNum_Flag){
		ucpcf_U=PCF8591_Adc()/51*10; 
		if(ucNumHL_Flag==1){
			 if(ucpcf_U>=0&&ucpcf_U<10){
				 ucNumH=50;
			 }
			 else if(ucpcf_U>=10&&ucpcf_U<20){
				 ucNumH=60;
			 }
			else if(ucpcf_U>=20&&ucpcf_U<30){
				 ucNumH=70;
			 }
			else if(ucpcf_U>=30&&ucpcf_U<40){
				 ucNumH=80;
			 }
			else if(ucpcf_U>=40&&ucpcf_U<50){
				 ucNumH=90;
			 }
		}
		else if(ucNumHL_Flag==2){
			 if(ucpcf_U>=0&&ucpcf_U<10){
				 ucNumL=0;
			 }
			 else if(ucpcf_U>=10&&ucpcf_U<20){
				 ucNumL=10;
			 }
			else if(ucpcf_U>=20&&ucpcf_U<30){
				 ucNumL=20;
			 }
			else if(ucpcf_U>=30&&ucpcf_U<40){
				 ucNumL=30;
			 }
			else if(ucpcf_U>=40&&ucpcf_U<50){
				 ucNumL=40;
			 }
		}
	}
		ucWave=Wave_Recv();
		if(ucWave<=ucNumH&&ucWave>=ucNumL){
			ucLed_Flag=0;
			ucWave_Flag=0;
		}
		else{
			ucLed_Flag=1;
			ucWave_Flag=1;
		}
		if(ucWave_Flag!=ucWave_Old){
			ucWave_Old=ucWave_Flag;
			if(ucWave_Flag==1){
				ucWave_Num++;
			}
		}
	switch(ucState){
		case 0:
			sprintf(pucSeg_Buf,"A    %3d",(unsigned int)ucWave);
			break;
		case 1:
			if(ucNum_Flag){
				sprintf(pucSeg_Buf,"P2 %2d-%2d",(unsigned int)ucNumL,(unsigned int)ucNumH);
			}
			else{
				sprintf(pucSeg_Buf,"P1 %2d-%2d",(unsigned int)ucNumL,(unsigned int)ucNumH);
			}
			
			break;
		case 2:
			if(ucWave_Num<=9){
				sprintf(pucSeg_Buf,"E      %1d",(unsigned int)ucWave_Num);
			}
			else{
				sprintf(pucSeg_Buf,"E      -");
			}
			break;
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
 void Num_Proc(unsigned char ucKey_Down){
	 if(ucState==1){
		 if(!ucNum_Flag){
			 if(ucKey_Down==9){
				 ucNumH+=10;
				 if(ucNumH==100) ucNumH=50;
			 }
			 else if(ucKey_Down==8){
				 ucNumL+=10; 
				 if(ucNumL==50) ucNumL=0;
			 }
		 }
		 else{
			 if(ucKey_Down==9){
				 ucNumHL_Flag=1;
			 }
			 else if(ucKey_Down==8){
				 ucNumHL_Flag=2;
			 }
		 }
	 }
 }
 void Led_Proc(void){
	 switch(ucState){
		 case 0:
			 ucLed&=~4;
			ucLed|=1;
			 break;
		  case 1:
			  ucLed&=~1;
			ucLed|=2;
			 break;
		   case 2:
			   ucLed&=~2;
			ucLed|=4;
			 break;
	 }
	 if(!ucLed_Flag){
		 ucLed|=0x80;
	 }
	 else{
		 if(ucLed_N){
			 ucLed|=0x80;
		 }
		 else{
			 ucLed&=~0x80;
		 }
	 }
 }